/**
 * 
 */
package control;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
 * @author LYM
 *
 */
public class InfoHost2 {
	
	public static final int TIMEOUT= 3000;
	
	private String hostname;
	private String hostaddr;
	private boolean reachable;
	private String cpuusage;
	private String mem;
	
	private static Session oSession = null;
	
	/**
	 * @param hostname
	 * @param hostaddr
	 */
	public InfoHost2(String hostname, String hostaddr) {
		this.hostname = hostname;
		this.hostaddr = hostaddr;
		this.reachable = false;
		this.cpuusage = null;
		this.mem = null;
	}

	/**
	 * @return the hostname
	 */
	public String getHostname() {
		return hostname;
	}

	/**
	 * @param hostname the hostname to set
	 */
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	/**
	 * @return the hostaddr
	 */
	public String getHostaddr() {
		return hostaddr;
	}

	/**
	 * @param hostaddr the hostaddr to set
	 */
	public void setHostaddr(String hostaddr) {
		this.hostaddr = hostaddr;
	}

	/**
	 * @return the reachable
	 */
	public boolean isReachable() {
		return reachable;
	}

	/**
	 * @param reachable the reachable to set
	 */
	public void setReachable(boolean reachable) {
		this.reachable = reachable;
	}

	/**
	 * @return the cpuusage
	 */
	public String getCpuusage() {
		return cpuusage;
	}

	/**
	 * @param cpuusage the cpuusage to set
	 */
	public void setCpuusage(String cpuusage) {
		this.cpuusage = cpuusage;
	}

	/**
	 * @return the mem
	 */
	public String getMem() {
		return mem;
	}

	/**
	 * @param mem the mem to set
	 */
	public void setMem(String mem) {
		this.mem = mem;
	}
	
	/**
	 * 
	 */
	public void getInfo(String username, String password){
		try {
			reachable= InetAddress.getByName(hostaddr).isReachable(TIMEOUT);
		} catch (UnknownHostException e) {
			System.out.println(e);
		} catch (IOException e) {
			System.out.println(e);
		}
		if(reachable){
		}
		try {
            JSch jsch = new JSch();
            oSession = jsch.getSession("ylisima", "melofee.enseeiht.fr", 22);
            oSession.setPassword("liniseL972");
 
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            oSession.setConfig(config);
 
            oSession.connect();
 
            executeCommand(
					"pwd"
			);
 
            oSession.disconnect();
        } catch (Exception e) {
            System.out.println(e);
        }
	}
	
	@Override
	public String toString() {
		return String.format("InfoHost [%s, %s, %s, %s]", hostname, reachable,
				cpuusage, mem);
	}
	
	/**
     * @param command
     * @return
     */
    public static int executeCommand(String command) {
        int exitCode = -1;
        ChannelExec channel = null;
        BufferedReader stdoutReader = null;
        BufferedReader stderrReader = null;
        try {
            channel = (ChannelExec) oSession.openChannel("exec");
 
            channel.setCommand(command + "\n");
 
            InputStream stdout = channel.getInputStream();
            InputStream stderr = channel.getErrStream();
 
            channel.connect();
 
            stdoutReader = new BufferedReader(new InputStreamReader(stdout));
            stderrReader = new BufferedReader(new InputStreamReader(stderr));
 
            while (stdoutReader.read() != -1)
                System.out.println(stdoutReader.readLine());
 
            exitCode = channel.getExitStatus();
 
            return exitCode;
        } catch (IOException e) {
            System.out.println("Cannot execute remote command: " + command
                    + " : " + e.getMessage());
        } catch (Exception e) {
            System.out.println("Cannot execute remote command: " + command
                    + " : " + e.getMessage());
        } finally {
            try {
                stdoutReader.close();
                stderrReader.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            if (channel != null) {
                channel.disconnect();
            }
        }
        return exitCode;
    }

	/**
	 * @param args
	 */
	public static void main(String args[]){
		InfoHost2 host= new InfoHost2("melofee", "melofee.enseeiht.fr");
		host.getInfo("ylisima", "liniseL972");
		System.out.println(host);
	}
}
